﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;
using Page = Cms.Domain.Page;

namespace Cms.Web.UI.Admin
{
    public partial class EditRssFeed : AdminPage, IEditRssFeedView
    {
        private EditRssFeedPresenter _presenter;

        #region IEditRssFeedView Members

        public int RssFeedID
        {
            get
            {
                if (ViewState["RssFeedID"] == null)
                    ViewState["RssFeedID"] = QueryString.GetInt32ID("id");
                return (int)ViewState["RssFeedID"];
            }
        }
        
        public void SetRssFeedID(int id)
        {
            ViewState["RssFeedID"] = id;
        }

        public string Name
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }

        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        public string Filename
        {
            get { return txtFilename.Text; }
            set { txtFilename.Text = value; }
        }

        public void SetFolders(string domain, IList<Folder> folders)
        {
            ddlFolders.Items.Clear();
            ddlFolders.Items.Add(new ListItem(domain, "-1"));
            SetFolderTree(1, folders);
        }
        
        public int SelectedFolderID
        {
            get { return int.Parse(ddlFolders.SelectedValue); }
            set { ddlFolders.SelectedValue = value.ToString(); }
        }

        public void SetPages(IList<Page> pages)
        {
            ddlPages.Items.Clear();
            foreach (Page p in pages)
            {
                ddlPages.Items.Add(new ListItem(p.Name, p.ID.ToString()));
            }
            ddlPages.Visible = pages.Count > 0;
            lblNoPages.Visible = pages.Count == 0;
        }

        public int SelectedPageID
        {
            get
            {
                if (lblNoPages.Visible)
                    return -1;
                return int.Parse(ddlPages.SelectedValue);
            }
            set { ddlPages.SelectedValue = value.ToString(); }
        }

        public void SetCategories(IList<Category> rootCategories, IList<Category> inheritedCategories,
                                  IList<Category> categories)
        {
            csCategories.SetCategories(rootCategories, inheritedCategories, categories);
        }

        public IEnumerable<Category> GetSelectedCategories()
        {
            return csCategories.GetSelectedCategories();
        }

        public RssFeedContentDisplayMode SelectedContentDisplayMode
        {
            get
            {
                return rbComplete.Checked ? RssFeedContentDisplayMode.Complete : RssFeedContentDisplayMode.Truncated;
            }
            set
            {
                switch (value)
                {
                    case RssFeedContentDisplayMode.Complete:
                        rbComplete.Checked = true;
                        break;
                    case RssFeedContentDisplayMode.Truncated:
                        rbTruncated.Checked = true;
                        break;
                }
            }
        }

        public bool RemoveImgTags
        {
            get { return chkRemoveImgTags.Checked; }
            set { chkRemoveImgTags.Checked = value; }
        }

        public ContentSortMode SortMode
        {
            get
            {
                return rbSortKey.Checked ? ContentSortMode.SortKey : ContentSortMode.PublishedTimestamp;
            }
            set
            {
                switch (value)
                {
                    case ContentSortMode.PublishedTimestamp:
                        rbPublishedTimestamp.Checked = true;
                        break;
                    case ContentSortMode.SortKey:
                        rbSortKey.Checked = true;
                        break;
                }
            }
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditRssFeed.xml"; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditRssFeedPresenter(this);
            HeadingText = GetText(this, RssFeedID > 0 ? "EditRssFeedHeading" : "NewRssFeedHeading");
            if (Page.IsPostBack)
                return;
            _presenter.Initialize();
            HeadingIcon = litIcon.Text;
            txtName.Focus();
        }


        protected void ddlFolders_Changed(object sender, EventArgs e)
        {
            _presenter.UpdatePages();
        }

        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (_presenter.IsValid)
            {
                Response.Redirect("RssFeeds.aspx");
            }
        }

        private void SetFolderTree(int level, IEnumerable<Folder> folders)
        {
            foreach (Folder f in folders)
            {
                string name = f.Name;
                for (int i = 0; i < level; i++)
                    name = "&nbsp;&nbsp;&nbsp;&nbsp;" + name;
                ddlFolders.Items.Add(new ListItem(name, f.ID.ToString()));
                SetFolderTree(level + 1, f.SubFolders);
            }
        }
    }
}